package com.yhcnet.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhcnet.mapper.UserMapper;
import com.yhcnet.po.Login;
import com.yhcnet.pojo.User;
import com.yhcnet.utils.RedisUtils;
import com.yhcnet.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 验证验证码
     * @param login
     * @return
     */
    public boolean codeCheck(Login login) {
        if(login == null) {return false;}

        String code = login.getCode().toLowerCase();
        String key = login.getKey();
        //获缓存
        if(!redisUtils.hHasKey("captcha",key)) {
            return false;
        }
        String val = (String) redisUtils.hget("captcha",key);
        if(val.equals(code)){return true;}

        return false;
    }


    /**
     * 用户登录
     * @param login
     * @return String uuid
     */
    public String login(Login login) {
        if(login == null) {return null;}

        //获取加密后的账号和密码
        String username = login.getUsername();
        String password = Utils.md5(login.getPassword().getBytes());
        // 账号密码验证
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("uid","password").eq("username", username);
        User user = userMapper.selectOne(wrapper);
        if(user != null && password.equals(user.getPassword())) {
            //设置缓存
            return setRedis(user.getUid());
        }

        return null;
    }

    /**
     * 设置用户登录有效期
     * @param uid
     * @return String uuid
     */
    public String setRedis(int uid) {
        if(uid <= 0) {return null;}

        String uuid = UUID.randomUUID().toString();

        boolean set = redisUtils.hset("user",uuid, uid, 60 * 60 * 8);

        if(set) return uuid;

        return null;
    }

    /**
     * 检查用户是否已经登录
     * @param uuid
     * @return
     */
    public boolean checkUser(String uuid) {
        return redisUtils.hHasKey("user",uuid);
    }


    /**
     * 注册
     */
    public boolean regist(Login regist) {
        User user = new User();
        user.setUid(0);
        user.setUsername(regist.getUsername());
        String md5 = Utils.md5(regist.getPassword().getBytes());
        user.setPassword(md5);
        user.setDate(null);
        user.setAvatar(null);

        int insert = userMapper.insert(user);

        return insert > 0;
    }

    /**
     * 检测用户是否可注册
     * @param username
     * @return
     */
    public boolean checkUserAlready(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        return user==null?false:true;
    }

    /**
     * 获取用户头像
     * @param uuid
     * @return
     */
    public String getAva(String uuid) {
        int uid = (Integer) redisUtils.hget("user", uuid);//用户id

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        User user = userMapper.selectOne(wrapper);

        return user.getAvatar();
    }
}
